Assignment: Named Volumes
An exercise for the Named Volumes
can be database upgrade. For example, we are using a postgres
of version 12.6
. Now there is a update to 13.2
. In this case, if we want to upgrade database, we should not loose the existing data.
Steps
- Run container with postgres 12.6
- Check the volumes
- Check logs and stop container
- Create another container with postgres 13.6 with same named volume
- Check logs to validate
To do so, when we create a database, we first have to define a Named Volumes
and when do the upgrade, we have to specify the data directory.
Let's first create a database with legacy version and specify the Named Volumes
,
docker run --name postgres_legacy -d -e POSTGRES_HOST_AUTH_METHOD=trust -v postgres_data:/var/lib/postgresql/data postgres:12.6
Here POSTGRES_HOST_AUTH_METHOD=trust
to allow all connections without a password. It's not a recommended approach. By default postgres put data in the /var/lib/postgresql/data
directory.
We can keep watching the logs of the server,
docker container logs -f postgres_legacy
Here, -f
allow to keep watching the logs.
We should see database system is ready to accept connections
in the database logs.
We can look at the volume list,
docker volume ls
We should see a volume named postgres_data
.
To upgrade to 13.2
, we first stop the previous version,
docker container stop postgres_legacy
Now create a new postgres server with updated version and configured the same Named Volume
,
docker run --name postgres_new -d -e POSTGRES_HOST_AUTH_METHOD=trust -v postgres_data:/var/lib/postgresql/data postgres:13.2
Since, we are using existing data directory, our server will start quickly with few logs,
docker container logs postgres_new
If we look into the volumes, we should there is no additional volume for the new updated postgres server, it is using the same as legacy one, postgres_data
docker volume ls